package com.cj.mobile.iabp.mvp.ui.fragment;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.dialog.PopupTipsDialog;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.app.global.Constant;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.di.component.DaggerMainComponent;
import com.cj.mobile.iabp.mvp.contract.MainContract;
import com.cj.mobile.iabp.mvp.model.entity.AppUpdate;
import com.cj.mobile.iabp.mvp.presenter.MainPresenter;
import com.cj.mobile.iabp.mvp.ui.activity.BasicInfoActivity;
import com.cj.mobile.iabp.mvp.ui.activity.LoginActivity;
import com.cj.mobile.iabp.mvp.ui.activity.PhotoViewActivity;
import com.cj.mobile.iabp.mvp.ui.activity.SettingActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的
 */
public class MyFragment extends BaseFragment<MainPresenter> implements MainContract.View {
    /*--------------------------------控件信息--------------------------------*/
    @BindView(R.id.fragment_my)
    View myLayout;                                                                                  // 总布局

    @BindView(R.id.civi_fragmentusertab_user_photo)
    ImageView userPhoto;                                                                            // 头像
    @BindView(R.id.txvi_fragmentusertab_name)
    TextView txviMobile;                                                                            // 手机号
    @BindView(R.id.txvi_fragmentusertab_department)
    TextView txviDepartment;                                                                        // 科室

    @BindView(R.id.btn_settingactivity_outlogin)
    Button btnOut;                                                                                  // 退出登录

    /*--------------------------------业务信息--------------------------------*/
    @Inject
    AccountManager mAccountManager;

    @Inject
    ImageLoader mImageLoader;
    // 退出登录 提示框
    private PopupTipsDialog popupTipsDialog;

    // 对话框
    private MaterialDialog mDialog;

    @Override
    public void onResume() {
        super.onResume();

        onTabReselect();
    }

    public void onTabReselect() {
        // 加载数据
        txviMobile.setText(mAccountManager.getUserName() + "(" + mAccountManager.getCurrPhone() + ")");
        txviDepartment.setText(mAccountManager.getDeptName());

        // 加载头像
        if (TextUtils.isEmpty(mAccountManager.getPhotoUrl())) {
            // 没值，则用本地图片
            if(mAccountManager.getSex().equals("2")){
                // 女
                userPhoto.setImageResource(R.mipmap.icon_female);
            }else {
                // 男
                userPhoto.setImageResource(R.mipmap.icon_male);
            }
        } else {
            // 有值，用网络图片
            mImageLoader.loadImage(getContext(),
                    ImageConfigImpl.builder().url(mAccountManager.getPhotoUrl())
                            .fallback(R.mipmap.icon_male)
                            .placeholder(R.mipmap.icon_male)
                            .imageRadius(10)
                            .imageView(userPhoto).build());
        }

    }

    @Override
    public void onDestroy() {
        if (mDialog != null) {
            this.mDialog.dismiss();
        }
        super.onDestroy();
        if (popupTipsDialog != null && popupTipsDialog.isShowing()) {
            popupTipsDialog.dismiss();
            popupTipsDialog = null;
        }
        this.mAccountManager = null;
        this.mImageLoader = null;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        // 初始化Loading对话框
        mDialog = new MaterialDialog.Builder(getContext()).content(R.string.common_execute).progress(true, 0).build();

        // 初始化弹出框
        popupTipsDialog = new PopupTipsDialog(getContext(), "确认退出当前账号？", click -> {
            if (click) {
                //清理缓存
                mAccountManager.clearAccountInfo();
                mAccountManager.setIsSwitchAccount(true);
                //界面跳转
                Bundle bundle = new Bundle();
                bundle.putBoolean("isStartLogin", false);
                ActivityUtils.startActivity(bundle, LoginActivity.class);
                killMyself();
            }
        });

    }

    @OnClick({
            R.id.civi_fragmentusertab_user_photo,                                                   // 头像
            R.id.lila_fragmentusertabmy_basicinfo,                                                  // 基本信息
            R.id.lila_fragmentusertab_setting,                                                      // 设置
            R.id.btn_settingactivity_outlogin,                                                      // 退出登录
    })
    @Override
    public void onClick(View v) {
        if (!CommonUtils.isDoubleClick()) {
            switch (v.getId()) {
                case R.id.civi_fragmentusertab_user_photo:                                          // 头像
                    if (!TextUtils.isEmpty(mAccountManager.getPhotoUrl())) {
                        Intent intent = new Intent(getContext(), PhotoViewActivity.class);
                        intent.putExtra(Constant.IMAGE_URL, mAccountManager.getPhotoUrl());
                        startActivity(intent);
                    }
                    break;
                case R.id.lila_fragmentusertabmy_basicinfo:                                         // 基本信息
                    ActivityUtils.startActivity(BasicInfoActivity.class);
                    break;
                case R.id.lila_fragmentusertab_setting:                                             // 设置
                    ActivityUtils.startActivity(SettingActivity.class);
                    break;
                case R.id.btn_settingactivity_outlogin:                                             // 退出登录
                    if (popupTipsDialog != null)
                        popupTipsDialog.showAtLocation(myLayout, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
                    break;
            }
        }
    }

    @Override
    public void showLoadingSubmit() {
        if (mDialog != null)
            mDialog.show();
    }

    @Override
    public void hideLoadingSubmit() {
        if (mDialog != null)
            mDialog.hide();
    }


    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    /**
     * 提示APP升级弹框，在这里不做任何处理
     */
    @Override
    public void askDialog(AppUpdate info) {

    }

    /**
     * 在这里不做任何处理
     */
    @Override
    public void onClickHome() {

    }

    @Override
    public void loadStatistics(int install, int remove, List<String> sec2, List<String> sec3, String[] title) {

    }
}
